package com.jtny.nytb.equipment.controller;

import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.jtny.component.entity.PageData;
import com.jtny.component.entity.PageParam;
import com.jtny.component.entity.WebResponse;
import com.jtny.nytb.equipment.constant.ResultConstant;
import com.jtny.nytb.equipment.enums.CodeRuleTypeEnum;
import com.jtny.nytb.equipment.service.RuleNumberService;
import com.jtny.nytb.equipment.vo.rule.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备编号规则管理Controller
 *
 * @author 智慧能源中心 - 伍天宇
 * @package com.jtny.nytb.equipment.controller
 * @fileName RuleNumberController.java
 * @createTime 2023年09月14日 16:18
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@RestController
@ResponseBody
@Api(value = "设备编号规则管理", tags = "设备编号规则管理")
@ApiSort(202303)
@RequestMapping("/rule-number")
@AllArgsConstructor
@Validated
public class RuleNumberController {

    private RuleNumberService ruleNumberService;

    @PostMapping("/query-page")
    @ApiOperation("分页查询设备编号规则")
    @ApiOperationSupport(order = 1)
    public WebResponse<PageData<CodeLevelPageVO>> queryPage(@RequestBody CodeLevelQueryVO codeLevelQueryVO) {
        PageData<CodeLevelPageVO> result = ruleNumberService.queryCodeLevelPage(CodeRuleTypeEnum.GENERAL.getType(), codeLevelQueryVO);
        return WebResponse.success(result);
    }

    @PostMapping("/query-level-page/{levelId}")
    @ApiOperation("分页查询设备编号规则")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParam(name = "levelId", value = "级别id", required = true, dataType = "Long", paramType = "path")
    public WebResponse<PageData<CodeRulePageVO>> queryLevelPage(@PathVariable Long levelId, @RequestBody PageParam pageParam) {
        PageData<CodeRulePageVO> result = ruleNumberService.queryCodeRulePage(CodeRuleTypeEnum.GENERAL.getType(), levelId, pageParam);
        return WebResponse.success(result);
    }


    /**
     * 获取设备编号级别
     *
     * @return {@link WebResponse}<{@link List}<{@link RuleNumberVO}>>
     */
    @ApiOperation("获取设备编号级别")
    @GetMapping("/getRuleLevel")
    @ApiOperationSupport(order = 12)
    public WebResponse<List<LevelNumberInfoVO>> getRuleLevel() {
        List<LevelNumberInfoVO> result = ruleNumberService.listLevel(CodeRuleTypeEnum.GENERAL.getType());
        return WebResponse.success(result);
    }


    /**
     * 前置条件添加 - 获取设备编号级别
     *
     * @return {@link WebResponse}<{@link List}<{@link RuleNumberVO}>>
     */
    @ApiOperation("获取设备编号级别")
    @GetMapping("/list-level/{nowLevelId}")
    @ApiOperationSupport(order = 12)
    public WebResponse<List<LevelNumberInfoVO>> listLevel(@PathVariable("nowLevelId") Long nowLevelId) {
        List<LevelNumberInfoVO> result = ruleNumberService.listLevelByLevelId(CodeRuleTypeEnum.GENERAL.getType(), nowLevelId);
        return WebResponse.success(result);
    }

    /**
     * 前置条件添加 - 所有级别字典
     *
     * @return {@link WebResponse}<{@link List}<{@link RuleNumberVO}>>
     */
    @ApiOperation("获取设备编号级别-前置条件添加")
    @GetMapping("/list-all-dict/{levelId}")
    public WebResponse<List<CodeRuleDictVO>> listAllDictByRuleLevel(@PathVariable("levelId") Long levelId) {
        List<CodeRuleDictVO> result = ruleNumberService.listAllDictByRuleLevel(CodeRuleTypeEnum.GENERAL.getType(), levelId);
        return WebResponse.success(result);
    }

    @ApiOperation("校验字典值")
    @PostMapping("/check-dict-value")
    public WebResponse<ValidDictVO> checkDictValue(@RequestBody @Validated ValidDictVO validDictVO) {
        ValidDictVO result = ruleNumberService.checkDictValue(CodeRuleTypeEnum.GENERAL.getType(), validDictVO);
        return WebResponse.success(result);
    }

    @PostMapping("/save")
    @ApiOperation("新增设备编号级别")
    @ApiOperationSupport(order = 3)
    public WebResponse<?> save(@RequestBody @Validated RuleNumberFormVO ruleNumberFormVO) {
        ruleNumberService.saveCodeLevel(CodeRuleTypeEnum.GENERAL.getType(), ruleNumberFormVO);
        return WebResponse.success(ResultConstant.OPERATE_SUCCESS_MESSAGE);
    }

    @PutMapping("/update")
    @ApiOperation("编辑设备编号级别")
    @ApiOperationSupport(order = 4)
    public WebResponse<?> update(@RequestBody @Validated(Update.class) RuleNumberFormVO ruleNumberFormVO) {
        ruleNumberService.updateCodeLevel(CodeRuleTypeEnum.GENERAL.getType(), ruleNumberFormVO);
        return WebResponse.success(ResultConstant.OPERATE_SUCCESS_MESSAGE);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除设备编号级别")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParam(name = "id", value = "设备编号级别id", required = true, dataType = "Long", paramType = "path")
    public WebResponse<?> delete(@PathVariable Long id) {
        ruleNumberService.deleteCodeLevel(CodeRuleTypeEnum.GENERAL.getType(), id);
        return WebResponse.success(ResultConstant.OPERATE_SUCCESS_MESSAGE);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("详情设备编号级别")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParam(name = "id", value = "设备编号级别id", required = true, dataType = "Long", paramType = "path")
    public WebResponse<LevelNumberInfoVO> detail(@PathVariable Long id) {
        LevelNumberInfoVO result = ruleNumberService.detailCodeLevel(CodeRuleTypeEnum.GENERAL.getType(), id);
        return WebResponse.success(result);
    }

    @PutMapping("move/{upOrDown}/{id}")
    @ApiOperation("上移/下移设备编号级别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "upOrDown", value = "1:上移/-1:下移", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "id", value = "设备编号级别id", required = true, dataType = "int", paramType = "path")
    })
    @ApiOperationSupport(order = 7)
    public WebResponse<?> move(@PathVariable Integer upOrDown, @PathVariable Long id) {
        ruleNumberService.moveCodeLevel(CodeRuleTypeEnum.GENERAL.getType(), upOrDown, id);
        return WebResponse.success(ResultConstant.OPERATE_SUCCESS_MESSAGE);
    }

    @PostMapping("save/rule")
    @ApiOperation("新增设备编号规则")
    @ApiOperationSupport(order = 8)
    public WebResponse<?> saveRule(@RequestBody @Validated RuleFormVO ruleFormVO) {
        ruleNumberService.saveCodeRule(CodeRuleTypeEnum.GENERAL.getType(), ruleFormVO);
        return WebResponse.success(ResultConstant.OPERATE_SUCCESS_MESSAGE);
    }

    @PutMapping("update/rule")
    @ApiOperation("编辑设备编号规则")
    @ApiOperationSupport(order = 9)
    public WebResponse<?> updateRule(@RequestBody @Validated(Update.class) RuleFormVO ruleFormVO) {
        ruleNumberService.updateCodeRule(CodeRuleTypeEnum.GENERAL.getType(), ruleFormVO);
        return WebResponse.success(ResultConstant.OPERATE_SUCCESS_MESSAGE);
    }

    @GetMapping("detail/rule/{id}")
    @ApiOperation("详情设备编号规则")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParam(name = "id", value = "设备编号规则id", required = true, dataType = "Long", paramType = "path")
    public WebResponse<RuleInfoVO> detailRule(@PathVariable Long id) {
        RuleInfoVO result = ruleNumberService.detailCodeRule(CodeRuleTypeEnum.GENERAL.getType(), id);
        return WebResponse.success(result);
    }

    @DeleteMapping("delete/rule/{id}")
    @ApiOperation("删除设备编号规则")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParam(name = "id", value = "设备编号规则id", required = true, dataType = "Long", paramType = "path")
    public WebResponse<?> deleteRule(@PathVariable Long id) {
        ruleNumberService.deleteCodeRule(CodeRuleTypeEnum.GENERAL.getType(), id);
        return WebResponse.success(ResultConstant.OPERATE_SUCCESS_MESSAGE);
    }


    /**
     * 获取指定级别设备编号规则
     *
     * @return {@link WebResponse}<{@link List}<{@link CodeRuleDictVO}>>
     */
    @ApiOperation("获取指定级别设备编号规则")
    @PostMapping("/getRuleNumber")
    @ApiImplicitParam(name = "levelId", value = "设备编号级别id", required = true, paramType = "path", dataType = "Long")
    @ApiOperationSupport(order = 13)
    public WebResponse<List<CodeRuleDictVO>> getRuleNumber(@RequestBody @Validated CodeRuleDictSelectQueryVO codeRuleDictSelectQueryVO) {
        return WebResponse.success(ruleNumberService.listRuleDictSelect(CodeRuleTypeEnum.GENERAL.getType(), codeRuleDictSelectQueryVO.getTargetLevelId(), codeRuleDictSelectQueryVO.getCodeRuleDictQueryList()));
    }

}
